//
//  favListViewController.swift
//  BaseSwift
//
//  Created by gh on 2019/10/5.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit

class favListViewController: UIViewController {

    var lastMenuBtn:UIButton? = nil
    var page = 1
    var curType:String = "PLACE"
    var dataArray:Array<FavVo> = Array()
    
    func sendRequest(){
        
        API.collectList(params: ["type":self.curType,"page":page.description]) { (result) in
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            let pageVo = result as! Page<FavVo>
            if self.page == 1{
                
                self.dataArray = pageVo.list
                
            }else{
                
                for item in pageVo.list {
                    
                    self.dataArray.append(item)
                }
            }
        
            if pageVo.list.count < 20{
                
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            }
            self.tableView.reloadData()
            
        }
        
    }
    
    lazy var menuView:UIView = {
        
        let bg = UIView()
        
        //菜单
        let containerView = UIView()
        bg.addSubview(containerView)
        containerView.backgroundColor = UIColor.white
        var temp = Array<UIView>()
        let tits = ["地点","线路","玩家产品"]
        
        for sindex in 0 ..< tits.count {
            
            let item = tits.get(at: sindex)
            let menuBtn = UIButton()
            menuBtn.tag = 9090+sindex
            menuBtn.setTitle(item, for: .normal)
            menuBtn.titleLabel?.font = UIFont.commonFont
            menuBtn.setTitleColor(UIColor.textTitleColorBlack, for: .normal)
            menuBtn.addTarget(self, action: #selector(menuClick), for: .touchUpInside)
            containerView.addSubview(menuBtn)
            let _ = menuBtn.sd_layout()?.heightIs(44)
            let bline = UIView()
            bline.backgroundColor = UIColor.GP_Orange_Color
            bline.tag = 1900
            menuBtn.addSubview(bline)
            if sindex == 2 {
                
                let _ = bline.sd_layout()?.widthIs(66)!.heightIs(4)!.bottomSpaceToView(menuBtn,6)?.centerXEqualToView(menuBtn)

                
            }else{
                
                let _ = bline.sd_layout()?.widthIs(34)!.heightIs(4)!.bottomSpaceToView(menuBtn,6)?.centerXEqualToView(menuBtn)

            }
            temp.append(menuBtn)
            if item == "地点" {
                
                bline.isHidden = false
                self.lastMenuBtn = menuBtn
                
            }else{
                
                bline.isHidden = true

            }
            
            
        }
        let _ = containerView.sd_layout()?.topSpaceToView(bg,0)?.widthRatioToView(bg,1)
        
        containerView.setupAutoWidthFlowItems(temp, withPerRowItemsCount: 3, verticalMargin: 0, horizontalMargin: 0, verticalEdgeInset: 0, horizontalEdgeInset: 0)
        
        return bg
        
        
    }()
    
    lazy var tableView:UITableView = {
        
        
        var tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = UIColor.clear
        tableView.tableFooterView = UIView()
        tableView.separatorColor = UIColor.clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(UINib.init(nibName: "FavCell", bundle: nil), forCellReuseIdentifier: "FavCell")
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.backgroundColor
        self.navigationView.setTitle("收藏")
        
        view.addSubview(menuView)
        let _ = menuView.sd_layout()?.widthRatioToView(view,1)!.heightIs(44)!.topSpaceToView(self.view,HT_StatusBarAndNavigationBarHeight)
        self.view.addSubview(tableView)
        let _ = tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: HT_StatusBarAndNavigationBarHeight+44, left: 0, bottom: HT_TabbarSafeBottomMargin, right: 0))
        
        
        let header = MJRefreshNormalHeader.init {
            
            self.page = 1
            self.sendRequest()
        }
        header?.stateLabel.isHidden = true
        header?.lastUpdatedTimeLabel.isHidden = true
        self.tableView.mj_header = header
        
        let footer = MJRefreshAutoNormalFooter.init {

            self.page += 1
            self.sendRequest()
        }
        self.tableView.mj_footer = footer
        sendRequest()
    }
    

    @objc func menuClick(btn:UIButton){
        
        let bline = btn.viewWithTag(1900)
        bline?.isHidden = false
        
        if lastMenuBtn != btn {
            
            let bline1 = lastMenuBtn!.viewWithTag(1900)
            bline1?.isHidden = true
            
        }
        if btn.tag == 9090 {
            
            self.curType = "PLACE"
            
        }else if btn.tag == 9091{
            
            self.curType = "ROUTE"
        }else{
            
            self.curType = "PRODUCT"
        }
        
        page = 1
        sendRequest()
        self.lastMenuBtn = btn
        
    }
}
extension favListViewController:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 112+12
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "FavCell") as! FavCell
        cell.setModel(mod: self.dataArray.get(at:indexPath.row)!)
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let mod = self.dataArray.get(at: indexPath.row)

        
        if self.curType == "PLACE" {
            
            API.home_category_detail(params: ["projectId":(mod?.pdtId)!]) { (result) in
                 
                 
                 let dvc = PlaceDetailVC()
                 dvc.cateVo = (result as! CategoryVo)
                 PUSH(from: self, to: dvc)
                 
             }
            
        }else if self.curType == "ROUTE"{
            

             let routevc = RouteViewController()
            var cate = CategoryVo()
            cate.id = mod?.pdtId
            cate.proName = mod?.pdtName
            routevc.curCategory = cate
             routevc.isOnlyOne = true
             PUSH(from: self, to: routevc)

            
        }else{
            
            API.playDetail(params: ["id":(mod?.pdtId)!]) { (result) in
                
                API.productCommentList(params: ["page":"1",
                                                "pdtId":(mod?.pdtId!)!]) { (pageVo) in
                    
                                                    
                                                    let page = pageVo as! Page<CommentVo>
                                                    let dvc = ProductDetailVC()
                                                    dvc.product = (result as! ProductVo)
                                                    
                                                    if page.list.count > 0{
                                                        
                                                        dvc.commentVo = page.list.first
                                                    }
                                                    PUSH(from: self, to: dvc)
                }
                

            }
            
            
        }
        
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        
        return true
    }
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        
        return .delete
    }
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        

        
        //创建“删除”事件按钮
          let delete = UITableViewRowAction(style: .normal, title: "删除") {
              action, index in
              //将对应条目的数据删除

            let mod = self.dataArray.get(at: index.row)
            
            API.collect(params: ["type":"NOCOL",
                                 "colType":self.curType,
                                 "colId":mod!.id!]) { (result) in
                
                
                                    ToastUtil.show(msg: "取消收藏成功")
                                    self.dataArray.remove(at: index.row)
                                    tableView.reloadData()
            }
            
          }
          delete.backgroundColor = UIColor.GP_Red_Color
           
          //返回所有的事件按钮
          return [delete]
    }
    
}
